package com.ww.editsms.util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader.TileMode;
import android.net.ConnectivityManager;

import com.ww.editsms.entity.Person;
import com.ww.editsms.entity.SMS;


public class SmsUtil {
	private static final int DAY = 1440;

	public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
		// Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
		// .getHeight(), Config.ARGB_8888);
		// Canvas canvas = new Canvas(output);
		//
		// final int color = 0xff424242;
		// final Paint paint = new Paint();
		// final Rect rect = new Rect(0, 0, bitmap.getWidth(),
		// bitmap.getHeight());
		// final RectF rectF = new RectF(rect);
		// final float roundPx = 12;
		//
		// paint.setAntiAlias(true);
		// canvas.drawARGB(0, 0, 0, 0);
		// paint.setColor(color);
		// canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
		//
		// paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		// canvas.drawBitmap(bitmap, rect, rect, paint);

		return bitmap;

	}

	public static boolean checkNet(Context context) {
		boolean flag = false;
		ConnectivityManager cwjManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (cwjManager.getActiveNetworkInfo() != null) {
			flag = cwjManager.getActiveNetworkInfo().isAvailable();
		}
		return flag;
	}

	// 获得圆角图片的方�?
	public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {

		Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
				.getHeight(), Config.ARGB_8888);
		Canvas canvas = new Canvas(output);

		final int color = 0xff424242;
		final Paint paint = new Paint();
		final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
		final RectF rectF = new RectF(rect);

		paint.setAntiAlias(true);
		canvas.drawARGB(0, 0, 0, 0);
		paint.setColor(color);
		canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(bitmap, rect, rect, paint);

		return output;
	}

	// 获得带�?影的图片方法
	public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {
		final int reflectionGap = 4;
		int width = bitmap.getWidth();
		int height = bitmap.getHeight();

		Matrix matrix = new Matrix();
		matrix.preScale(1, -1);

		Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2,
				width, height / 2, matrix, false);

		Bitmap bitmapWithReflection = Bitmap.createBitmap(width,
				(height + height / 2), Config.ARGB_8888);

		Canvas canvas = new Canvas(bitmapWithReflection);
		canvas.drawBitmap(bitmap, 0, 0, null);
		Paint deafalutPaint = new Paint();
		canvas
				.drawRect(0, height, width, height + reflectionGap,
						deafalutPaint);

		canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

		Paint paint = new Paint();
		LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
				bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff,
				0x00ffffff, TileMode.CLAMP);
		paint.setShader(shader);
		// Set the Transfer mode to be porter duff and destination in
		paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
		// Draw a rectangle using the paint with our linear gradient
		canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()
				+ reflectionGap, paint);

		return bitmapWithReflection;
	}

	public static String getdays(long callTime) {
		String value = "";
		long newTime = new Date().getTime();
		long duration = (newTime - callTime) / (1000 * 60);
		if (duration < 1) {
			value = (newTime - callTime) / (1000) + "秒前";
		} else if (duration < 60) {
			value = duration + "分钟前";
		} else if (duration >= 60 && duration < DAY) {
			value = (duration / 60) + "小时前";
		} else if (duration >= DAY && duration < DAY * 2) {
			SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
			value = "昨天" + sdf.format(new Date(callTime));
		} else if (duration >= DAY * 2 && duration < DAY * 3) {
			SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
			value = "前天" + sdf.format(new Date(callTime));
		} else {
			SimpleDateFormat sdf = new SimpleDateFormat("yy-M-dd HH:mm");
			value = sdf.format(new Date(callTime));
		}
		return value;
	}

	public static String getDesc(String str) {
		if (str.length() > 30) {
			return str.substring(0, 30) + "...";
		} else {
			return str;
		}
	}

	/**
	 * 判断�?null,"","null" 均视为空.
	 * 
	 * @param str
	 *            字符�?
	 * @return 结果 boolean
	 */
	public static boolean isNotEmpty(String str) {
		boolean bool = true;
		if (str == null || "null".equals(str) || "".equals(str)) {
			bool = false;
		} else {
			bool = true;
		}
		return bool;
	}

	public static List<SMS> filter(List<SMS> list, String num) {
		List<SMS> filter = new ArrayList<SMS>();
		for (int i = 0; i < list.size(); i++) {
			SMS s = list.get(i);
			if (list.get(i).getBody().contains(num)) {
				s.setFilter(1);
			}else{
				s.setFilter(0);
			}
			filter.add(s);
		}
		return filter;
	}
	
	public static List<Person> filterPerson(List<Person> list, String num) {
		List<Person> filter = new ArrayList<Person>();
		for (int i = 0; i < list.size(); i++) {
			Person s = list.get(i);
			if (list.get(i).getName().contains(num)||list.get(i).getNumber().contains(num)||list.get(i).getCount().contains(num)) {
				filter.add(s);
			}
		}
		return filter;
	}
	
	

	public static List<SMS> filter(List<SMS> list) {
		List<SMS> filter = new ArrayList<SMS>();
		for (int i = 0; i < list.size(); i++) {
			SMS s = list.get(i);
			s.setFilter(0);
			filter.add(s);
		}
		return filter;
	}

}
